package com.example.administrator.x2picturetransliteration.Home.Fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.x2picturetransliteration.Base.BaseFragment;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Bean.CarouselListBean;
import com.example.administrator.x2picturetransliteration.Bean.UserDataBean;
import com.example.administrator.x2picturetransliteration.Constants;
import com.example.administrator.x2picturetransliteration.Home.Activity.AboutUsActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.AgreementActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.BuyingMembersActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.WCnmbActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.CommonProblemActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.ConsumptionActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.DraftsActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.FeedbackActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.SecurityManagementActivity;
import com.example.administrator.x2picturetransliteration.Home.Activity.ToUpdateActivity;
import com.example.administrator.x2picturetransliteration.Home.Dialog.OkDialog;
import com.example.administrator.x2picturetransliteration.Home.Dialog.ShareDialog;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Fragment.MePresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.AppCacheUtil;
import com.example.administrator.x2picturetransliteration.Util.Img.PicassoEqualProportionTransformation2;
import com.example.administrator.x2picturetransliteration.Util.Img.PicassoFilletTransformation;
import com.example.administrator.x2picturetransliteration.Util.Img.TailorImageView;
import com.example.administrator.x2picturetransliteration.Util.TimeStampUtil;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;
import com.example.administrator.x2picturetransliteration.Util.UmengShareUtil;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的
 * Created by Administrator on 2019/8/16.
 */

public class MeFragment extends BaseFragment{

    @BindView(R.id.banner)Banner banner;

    @BindView(R.id.nameImg)ImageView nameImg; //用户头像
    @BindView(R.id.userName)TextView userName; //用户名字
    @BindView(R.id.idText)TextView idText; //用户id(手机号)
    @BindView(R.id.DataText)TextView DataText; //vip到期时间

    @BindView(R.id.vipText)TextView vipText; //是否开通vip会员

    @BindView(R.id.TitleText)TextView TitleText;

    private MePresenter mMePresenter;


    @Override
    protected int intiLayout() {
        return R.layout.home_fragment_me;
    }

    @Override
    public void operation(View view) {

        TitleText.setText("我的");

        mMePresenter = new MePresenter(getContext(),mCompositeSubscriptions);

        banner.setImageLoader(new GlideImageLoader());

    }

    @Override
    public void LazyLoad() {

        UserStyle();
    }

    private void UserStyle(){


        /** 获取轮播图数据 */
        mMePresenter.carouselList(new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                List<CarouselListBean> CarouselListData = (List<CarouselListBean>)data;

                banner.setVisibility(CarouselListData.size()==0?View.GONE:View.VISIBLE);
                banner.setImages(CarouselListData);
                banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
                banner.start();

            }
        });

        mMePresenter.info(new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                Constants.getConstants().setmUserDataBean(JSON.parseObject(data + "", new TypeReference<UserDataBean>() {}));

                if (Constants.getConstants().getmUserDataBean()!=null){

                    if (Constants.getConstants().getmUserDataBean().getHeadPic()!=null){

                        if (Constants.getConstants().getmUserDataBean().getHeadPic()!=null && !"".equals(Constants.getConstants().getmUserDataBean().getHeadPic()))
                        Picasso.with(MeFragment.this.getContext())
                                .load(Constants.getConstants().getmUserDataBean().getHeadPic())
                                .transform(new TailorImageView())
                                .transform(new PicassoEqualProportionTransformation2(nameImg))
                                .placeholder(R.drawable.home_moren)
                                .error(R.drawable.home_moren)
                                .into(nameImg);

                    }

                    if (Constants.getConstants().getmUserDataBean().getNickname()!=null && !"".equals(Constants.getConstants().getmUserDataBean().getNickname())){

                        userName.setText(Constants.getConstants().getmUserDataBean().getNickname());

                    }else {

                        userName.setText("1".equals(Constants.getConstants().getmUserDataBean().getVipType())?"会员用户":"普通用户");
                    }

                    idText.setText("ID" + Constants.getConstants().getmUserDataBean().getId());

                    if ("1".equals(Constants.getConstants().getmUserDataBean().getVipType()) && Constants.getConstants().getmUserDataBean().getVipTime()!=null){

                        try {

                            if("3".equals(Constants.getConstants().getmUserDataBean().getVipType())){

                                DataText.setText("永久会员");

                            }else {

                                DataText.setText("有效期:" + new TimeStampUtil().dateToMonth(Constants.getConstants().getmUserDataBean().getVipTime()) + "到期");
                            }

                        }catch (Exception e){

                        }

                    }else {

                        DataText.setText("");
                    }

                    vipText.setText("1".equals(Constants.getConstants().getmUserDataBean().getVipType())?"续费VIP会员":"开通VIP会员");
                    if("3".equals(Constants.getConstants().getmUserDataBean().getVipType()))vipText.setText("永久会员");

                }

            }
        });

    }

    /** 轮播图适配器 */
    private class GlideImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {

            final CarouselListBean map = (CarouselListBean)path;

            if (map.getImg()!=null && !"".equals(map.getImg())){

                Picasso.with(MeFragment.this.getContext())
                        .load(map.getImg())
                        .placeholder(R.drawable.me_brean)
                        .error(R.drawable.me_brean)
                        .transform(new PicassoEqualProportionTransformation2(imageView))
                        .transform(new PicassoFilletTransformation(20))
                        .into(imageView);

            }else {

                Picasso.with(MeFragment.this.getContext())
                        .load(R.drawable.me_brean)
                        .placeholder(R.drawable.me_brean)
                        .error(R.drawable.me_brean)
                        .transform(new PicassoEqualProportionTransformation2(imageView))
                        .transform(new PicassoFilletTransformation(20))
                        .into(imageView);
            }

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(map.getUrl()!=null){

                        AgreementActivity.startAgreementActivity(MeFragment.this.getContext(),"个人中心",map.getUrl());
                        mMePresenter.Statistics("3");
                    }
                }
            });
        }

    }

    /** 常见问题 */
    @OnClick(R.id.DoubtImg)
    public void DoubtImgClick(){

        getContext().startActivity(new Intent(getContext(),CommonProblemActivity.class));
    }

    /** 草稿箱 */
    @OnClick(R.id.cgView)
    public void cgViewClick(){

        getContext().startActivity(new Intent(getContext(), DraftsActivity.class));
    }

    /** 消费记录 */
    @OnClick(R.id.xfView)
    public void xfViewClick(){

        getContext().startActivity(new Intent(getContext(), ConsumptionActivity.class));
    }

    /** 安全管理 */
    @OnClick(R.id.aqView)
    public void aqViewClick(){

        getContext().startActivity(new Intent(getContext(),SecurityManagementActivity.class));
    }

    /** 关于我们 */
    @OnClick(R.id.gyView)
    public void gyViewClick(){

        getContext().startActivity(new Intent(getContext(),AboutUsActivity.class));
    }

    /** 意见反馈 */
    @OnClick(R.id.yjView)
    public void yjViewClick(){

        getContext().startActivity(new Intent(getContext(), FeedbackActivity.class));
    }

    /** 检查更新 */
    @OnClick(R.id.jcView)
    public void jcViewClick(){

        getContext().startActivity(new Intent(getContext(),ToUpdateActivity.class));
    }

    /** 分享软件 */
    @OnClick(R.id.fxView)
    public void fxViewClick(){

        new ShareDialog(getContext(), new ShareDialog.hd() {
            @Override
            public void hdff(SHARE_MEDIA type, String word) {

                Share(type,"https://a.app.qq.com/o/simple.jsp?pkgname=com.example.administrator.x2picturetransliteration");
            }
        }).show();
    }

    /** 清除缓存 */
    @OnClick(R.id.qcView)
    public void qcViewClick(){

        AppCacheUtil.clearAllCache(getContext());
        new ToastUtil(getContext()).getToast(false,"缓存清理完毕").show();
    }

    /** 购买vip */
    @OnClick(R.id.vipView)
    public void vipViewClick(){

        getContext().startActivity(new Intent(getContext(),BuyingMembersActivity.class));
    }

    /** 退出登录 */
    @OnClick(R.id.tcText)
    public void tcTextClick(){

        new OkDialog(getContext(), "是否退出登录", new OkDialog.hd() {
            @Override
            public void hdff() {

                mMePresenter.updateUserDao();
            }
        }).show();

    }

    /** 协议说明 */
    @OnClick(R.id.cnmb)
    public void cnmbClick(){

        startActivity(new Intent(getContext(),WCnmbActivity.class));
    }

    private void Share(SHARE_MEDIA mSHARE_MEDIA,String url){

        UmengShareUtil mUmengShareUtil = new UmengShareUtil((Activity)getContext());
        mUmengShareUtil.setUrl(url);
        mUmengShareUtil.setTitle("图片扫描软件");
        mUmengShareUtil.setThumb(R.mipmap.home_logo);
        mUmengShareUtil.setDescription("图片转文字，word转识别扫描，表格识别扫描");
        mUmengShareUtil.Share(mSHARE_MEDIA);
    }
}
